﻿using System;
using System.Collections.Generic;

using System.Text;
using RoughJs.Ast;

namespace RoughJs.Error
{
    public class JavaScriptException : RhinoException
    {

        /**
         * @deprecated
         * Use {@link WrappedException#WrappedException(Throwable)} to report
         * exceptions in Java code.
         */
        public JavaScriptException(Object value)
            : this(value, "", 0)
        {

        }

        /**
         * Create a JavaScript exception wrapping the given JavaScript value
         *
         * @param value the JavaScript value thrown.
         */
        public JavaScriptException(Object value, String sourceName, int lineNumber)
        {
            recordErrorOrigin(sourceName, lineNumber, null, 0);
            this.value = value;
        }


        public String details()
        {
            try
            {
                return ScriptRuntime.toString(value);
            }
            catch (Exception rte)
            {
                // ScriptRuntime.toString may throw a RuntimeException
                if (value == null)
                {
                    return "null";
                }
                else if (value is Scriptable)
                {
                    return ScriptRuntime.defaultObjectToString((Scriptable)value);
                }
                else
                {
                    return value.ToString();
                }
            }
        }

        /**
         * @return the value wrapped by this exception
         */
        public Object getValue()
        {
            return value;
        }

        /**
         * @deprecated Use {@link RhinoException#sourceName()} from the super class.
         */
        public String getSourceName()
        {
            return sourceName();
        }

        /**
         * @deprecated Use {@link RhinoException#lineNumber()} from the super class.
         */
        public int getLineNumber()
        {
            return lineNumber();
        }

        private Object value;
    }

}
